package org.qiuqiu.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.qiuqiu.pojo.Group;
import org.qiuqiu.tools.SqlMapTool;

/**
 * @author 杜秋野
 * 
 */
public class GroupDao {

	/**
	 * 该方法为添加群组的方法
	 * 
	 * @param group
	 * @return
	 * @throws SQLException
	 */
	public boolean addGroup(Group group) throws SQLException {
		boolean result = false;
		try {
			SqlMapTool.getSqlMap().startTransaction();
			SqlMapTool.getSqlMap().insert("insertGroup", group);
			SqlMapTool.getSqlMap().commitTransaction();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
		} finally {
			SqlMapTool.getSqlMap().endTransaction();
		}
		return result;
	}

	/**
	 * 该方法修改指定群组的信息
	 * 
	 * @param group
	 * @return
	 * @throws SQLException
	 */
	public boolean modGroup(Group group) throws SQLException {
		boolean result = false;
		try {
			SqlMapTool.getSqlMap().startTransaction();
			SqlMapTool.getSqlMap().update("modGroup", group);
			SqlMapTool.getSqlMap().commitTransaction();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
		} finally {
			SqlMapTool.getSqlMap().endTransaction();
		}
		return result;
	}

	/**
	 * 该方法删除指定的群组
	 * 
	 * @param list
	 * @return
	 * @throws SQLException
	 */
	public boolean delGroup(List<Integer> list) throws SQLException {
		boolean result = false;
		try {
			SqlMapTool.getSqlMap().startTransaction();
			SqlMapTool.getSqlMap().startBatch();
			for (int i : list) {
				SqlMapTool.getSqlMap().delete("delGroup", i);
			}
			SqlMapTool.getSqlMap().executeBatch();
			SqlMapTool.getSqlMap().commitTransaction();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
		} finally {
			SqlMapTool.getSqlMap().endTransaction();
		}
		return result;
	}

	/**
	 * 将指定群组的用户移动到指定的另一个群组
	 * 
	 * @param oldGroup
	 * @param newGroup
	 * @return
	 * @throws SQLException
	 */
	public boolean moveGroup(int oldGroup, int newGroup) throws SQLException {
		boolean result = false;
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("oldGroup", oldGroup);
		map.put("newGroup", newGroup);
		try {
			SqlMapTool.getSqlMap().startTransaction();
			SqlMapTool.getSqlMap().update("moveGroup", map);
			SqlMapTool.getSqlMap().commitTransaction();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
		} finally {
			SqlMapTool.getSqlMap().endTransaction();
		}
		return result;
	}

	/**
	 * 该方法用于查询某个用户的指定名称的群组
	 * 
	 * @param group
	 *            group中包含了所要查询的用户的userId以及群组的名称groupName
	 * @return
	 * @throws SQLException
	 * @throws SQLException
	 */
	public Group getGroup(Group group) throws SQLException {
		Group g = (Group) SqlMapTool.getSqlMap().queryForObject("getGroup",
				group);
		return g;
	}

	/**
	 * 该方法用于查询某个用户的群组或某个指定的群组信息
	 * 
	 * @param userId
	 *            表示要查询的某个用户的ID
	 * @param groupId
	 *            表示要查询的某个群组的ID
	 * @return
	 * @throws SQLException
	 */
	public List<HashMap> getGroupInfo(int userId, int groupId)
			throws SQLException {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("userId", userId);
		map.put("groupId", groupId);
		List<HashMap> hm = SqlMapTool.getSqlMap().queryForList("getGroupInfo",
				map);
		return hm;
	}

	/*
	 * public static void main(String args[]){ GroupDao gd = new GroupDao(); try
	 * { List<HashMap> l = gd.getGroupInfo(6);
	 * System.out.println("groupId\tgroupName\tdescribe\tcount"); for(HashMap m
	 * : l){
	 * System.out.println(m.get("groupId")+"\t"+m.get("groupName")+"\t"+m.get
	 * ("describe")+"\t"+m.get("count")); } } catch (SQLException e) { // TODO
	 * Auto-generated catch block e.printStackTrace(); } }
	 */
}
